package com.mycompany.inventory.viewmodel;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.Event;

import com.mycompany.inventory.persistence.ProductCategoriesDAO;
import com.mycompany.inventory.persistence.ProductDAO;
import com.mycompany.inventory.pojo.Product;
import com.mycompany.inventory.pojo.ProductCategories;

public class CategoryViewModel implements org.zkoss.zk.ui.event.EventListener{
	private String id;
	private String name;
	private boolean parentId;
	private ProductCategories selectedCategory;
	private List<ProductCategories> categories = new ArrayList<ProductCategories>();
	private ProductCategoriesDAO dao = new ProductCategoriesDAO();
	private List<Product> products = new ArrayList<Product>();
	private ProductDAO pDao = new ProductDAO();

	public void onEvent(Event evt) throws InterruptedException {
        if (evt.getName().equals("onOK")) {

        } else {
        	System.out.println("###Target: " + evt.getTarget()); 
        	return;
        }
    }

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the parentId
	 */
	public boolean isParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(boolean parentId) {
		this.parentId = parentId;
	}
	
	/**
	 * @return the selectedCategory
	 */
	public ProductCategories getSelectedCategory() {
		return selectedCategory;
	}

	/**
	 * @param selectedCategory the selectedCategory to set
	 */
	public void setSelectedCategory(ProductCategories selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	/**
	 * @return the categories
	 */
	public List<ProductCategories> getCategories() {
		return categories;
	}

	/**
	 * @param categories the categories to set
	 */
	public void setCategories(List<ProductCategories> categories) {
		this.categories = categories;
	}
	
	/**
	 * @return the products
	 */
	public List<Product> getProducts() {
		return products;
	}

	/**
	 * @param products the products to set
	 */
	public void setProducts(List<Product> products) {
		this.products = products;
	}

	@Init
	public void init(){
		this.refeshCategoryGrid();
	}
	
	public void refeshCategoryGrid(){
		this.categories = this.dao.findAll();
	}
	
	@NotifyChange("products")
	@Command
	public void productsByCategory(){
		this.products = this.pDao.getProductByCategoryId(this.selectedCategory.getId());
	}
}